package avicit.bdp.dds.server.worker.task.sqoop.generator.sources;

import avicit.bdp.dds.dispatch.task.sqoop.SqoopParameters;
import avicit.bdp.dds.dispatch.task.sqoop.sources.SourceHiveParameter;
import avicit.bdp.core.util.json.JSONUtils;
import avicit.bdp.dds.server.entity.TaskExecutionContext;
import avicit.bdp.dds.server.worker.task.sqoop.generator.ISourceGenerator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * param source generator
 */
public class HiveSourceGenerator implements ISourceGenerator {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public String generate(SqoopParameters sqoopParameters, TaskExecutionContext taskExecutionContext) {
        StringBuilder sb = new StringBuilder();
        try{
            SourceHiveParameter sourceHiveParameter
                    = JSONUtils.parseObject(sqoopParameters.getSourceParams(),SourceHiveParameter.class);
            if(sourceHiveParameter != null){
                if(StringUtils.isNotEmpty(sourceHiveParameter.getHiveDatabase())){
                    sb.append(" --hcatalog-database ").append(sourceHiveParameter.getHiveDatabase());
                }

                if(StringUtils.isNotEmpty(sourceHiveParameter.getHiveTable())){
                    sb.append(" --hcatalog-table ").append(sourceHiveParameter.getHiveTable());
                }

                if(StringUtils.isNotEmpty(sourceHiveParameter.getHivePartitionKey())&&
                        StringUtils.isNotEmpty(sourceHiveParameter.getHivePartitionValue())){
                    sb.append(" --hcatalog-partition-keys ").append(sourceHiveParameter.getHivePartitionKey())
                            .append(" --hcatalog-partition-values ").append(sourceHiveParameter.getHivePartitionValue());
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage());
        }

        return sb.toString();
    }
}
